package com.yang.springcloud.dao;

import com.yang.springcloud.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class MemberImp implements MemberDao{

    @Autowired
    private JdbcTemplate jt;

    @Override
    public Map<String, Object> insertMemberInfo(List<Member> imList) {

        for (Member im : imList) {
            try {
                jt.update("insert into `member` (id, name)values (?,?)", im.getId(), im.getName());
            }catch (Exception e){

            }
            List<Meal> ilList = im.getMeallist();
            for (Meal il : ilList) {
                try {
                    jt.update("insert into meal(id, name)values (?,?)", il.getId(), il.getName());
                }catch (Exception e){

                }
                try{
                    jt.update("insert into member_meals(member_id, meal_id)values (?,?)", im.getId(), il.getId());
                }catch (Exception e){

                }

                List<Project> ijList = il.getProjectList();
                for (Project ij : ijList) {
                    try {
                        jt.update("insert into project(id, name, type_id)values (?,?,?)", ij.getId(), ij.getName(), ij.getType().getId());
                    }catch (Exception e){

                    }
                    try {
                        jt.update("insert into project_type(id, name)values (?,?)", ij.getType().getId(), ij.getType().getName());
                    }catch (Exception e) {

                    }
                    try {
                        jt.update("insert into meal_projects(meal_id, project_id)values (?,?)", il.getId(), ij.getId());
                    }catch (Exception e){

                    }
                }

                List<Product> idList = il.getProductList();
                for (Product id : idList) {
                    try {
                        jt.update("insert into product(id, name, type_id)values (?,?,?)", id.getId(), id.getName(), id.getType().getId());
                    }catch (Exception e){

                    }
                    try {
                        jt.update("insert into product_type(id, name)values (?,?)", id.getType().getId(), id.getType().getName());
                    }catch (Exception e){

                    }
                    try{
                        jt.update("insert into meal_products(meal_id, product_id)values (?,?)", il.getId(), id.getId());
                    }catch (Exception e){

                    }
                }
            }
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入成功");
        msg.put("code", 200);
        return msg;
    }

    @Override
    public List<Member> queryMemberAll() {
        RowMapper<Member> mper = new BeanPropertyRowMapper<Member>(Member.class);
        List<Member> qmList = jt.query("select * from `member`", mper);
        System.out.println(qmList);

        for(Member qm:qmList){
            RowMapper<Meal> lper = new BeanPropertyRowMapper<Meal>(Meal.class);
            List<Meal> qlList = jt.query("SELECT * FROM meal INNER JOIN member_meals ON member_meals.meal_id=meal.id AND member_meals.member_id = ?", lper, qm.getId());
            System.out.println(qlList);
            for (Meal ql:qlList){
                RowMapper<Project> jper = new BeanPropertyRowMapper<Project>(Project.class);
                List<Project> qjList = jt.query("SELECT * FROM project INNER JOIN meal_projects ON meal_projects.project_id=project.id AND meal_projects.meal_id = ?", jper, ql.getId());
                System.out.println(qjList);
                for(Project qj:qjList){
                    RowMapper<ProjectType>  jtper = new BeanPropertyRowMapper<ProjectType>(ProjectType.class);
                    ProjectType pjt =jt.queryForObject("SELECT * FROM project_type INNER JOIN project ON project_type.id=project.type_id AND project.id = ?", jtper, qj.getId());
                    qj.setType(pjt);
                    ql.addProject(qj);
                }

                RowMapper<Product> dper = new BeanPropertyRowMapper<Product>(Product.class);
                List<Product> qdList = jt.query("SELECT * FROM product INNER JOIN meal_products ON meal_products.product_id=product.id AND meal_products.meal_id = ?", dper, ql.getId());
                System.out.println(qdList);
                for (Product qd:qdList){
                    RowMapper<ProductType> dtper = new BeanPropertyRowMapper<ProductType>(ProductType.class);
                    ProductType pdt =jt.queryForObject("SELECT * FROM product_type INNER JOIN product ON product_type.id=product.type_id AND product.id = ?",dtper , qd.getId());
                    qd.setType(pdt);
                    ql.addProduct(qd);
                }
                qm.addMeal(ql);
            }
        }

        return qmList;
    }
}
